﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SuperFrame.Charts
{
    /// <summary>
    /// 指针控件
    /// </summary>
    public partial class ArrowsControl : UserControl
    {
        public ArrowsControl()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        private double offsetValue=0d;

        public double OffsetValue
        {
            get { return offsetValue; }
            set { offsetValue = value; }
        }

        private bool isRetrorse=true;
        /// <summary>
        /// 旋转方向 True 逆时针 false 顺时针
        /// </summary>
        public bool IsRetrorse
        {
            get { return isRetrorse; }
            set { isRetrorse = value; }
        }
        
        public Brush StrokeColor
        {
            get { return (Brush)GetValue(StrokeColorProperty); }
            set { SetValue(StrokeColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for StrokeColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StrokeColorProperty =
            DependencyProperty.Register("StrokeColor", typeof(Brush), typeof(ArrowsControl), new PropertyMetadata(Brushes.Red));

        private double _timeValue = 1;
        /// <summary>
        /// 指针完成一个旋转变化的耗时 默认1s
        /// </summary>
        public double VimeValue
        {
            get { return _timeValue; }
            set { _timeValue = value; }
        }

        public double AngleValue
        {
            get { return (double)GetValue(AngleValueProperty); }
            set { SetValue(AngleValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for AngleValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AngleValueProperty =
            DependencyProperty.Register("AngleValue", typeof(double), typeof(ArrowsControl), new PropertyMetadata(0d, new PropertyChangedCallback(AngleValueChange)));

        public static void AngleValueChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as ArrowsControl).StoryboardPlay(e);
        }
        protected void StoryboardPlay(DependencyPropertyChangedEventArgs e)
        {
            DoubleAnimation da = new DoubleAnimation();
            var value=double.Parse(e.NewValue.ToString());
            da.To = IsRetrorse ? OffsetValue - value : OffsetValue + value;
            da.Duration = new Duration(TimeSpan.FromSeconds(VimeValue));
            rotateTransform.BeginAnimation(RotateTransform.AngleProperty, da);
        }
    }
}
